/*
 *  nautilus-svn-checkout.c - Subversion extension for Nautilus
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Jack Xu Hong <jack.xu.hong@gmail.com>
 *
 */

#include <config.h>

#include <gtk/gtk.h>
#include <glade/glade.h>

#include <svn_pools.h>
#include <svn_client.h>
#include <svn_cmdline.h>

#include "nautilus-svn-browser.h"
#include "nautilus-svn-checkout.h"

typedef struct {
	GtkWidget *window;
	GtkComboBoxEntry *combo_repo_url;
	GtkComboBoxEntry *combo_local_dir;
	GtkRadioButton *radio_head;
	GtkRadioButton *radio_revision;
	GtkEntry *entry_revision;
	GtkButton *button_select_revision;
} NautilusSvnCheckout;


static void button_ok_clicked_cb            (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog);

static void button_close_clicked_cb         (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog);

static void button_browse_dir_clicked_cb    (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog);

static void radio_group_toggled_cb          (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog);

static void checkout_destroy_cb             (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog);



static void button_ok_clicked_cb            (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog)
{
	apr_pool_t *pool;
	svn_error_t *err;
	svn_client_ctx_t *ctx;

	gchar *repo_url = gtk_combo_box_get_active_text (
			GTK_COMBO_BOX (dialog->combo_repo_url));
	gchar *local_dir = gtk_combo_box_get_active_text(
			GTK_COMBO_BOX (dialog->combo_local_dir));

	svn_cmdline_init("nautilus-svn", stdout);

	pool = svn_pool_create(NULL);

	err = svn_client_create_context (&ctx, pool);

	//ctx->progress_func =
	svn_client_checkout2(NULL, repo_url, local_dir, NULL, NULL, 0, 0, ctx, pool);

	svn_pool_destroy(pool);

	g_free (local_dir);
	g_free (repo_url);

	gtk_widget_destroy (dialog->window);
}

static void button_close_clicked_cb         (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog)
{
	gtk_widget_destroy (dialog->window);
}

static void button_browse_repo_clicked_cb (GtkWidget *widget,
                                           NautilusSvnCheckout *dialog)
{
	nautilus_svn_browser_show (dialog->window, NULL);
}

static void button_browse_dir_clicked_cb    (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog)
{
	char* dir;

	GtkWidget *dir_chooser = gtk_file_chooser_dialog_new (
			"Select working directory",
			GTK_WINDOW (dialog->window),
			GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
			NULL);

	if (gtk_dialog_run (GTK_DIALOG (dir_chooser)) == GTK_RESPONSE_ACCEPT) {
		dir = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dir_chooser));

		gtk_entry_set_text (
				GTK_ENTRY (GTK_BIN (dialog->combo_local_dir)->child), dir);

		g_free (dir);
	}

	gtk_widget_destroy (dir_chooser);
}

static void radio_group_toggled_cb          (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog)
{
	gboolean head_selected = gtk_toggle_button_get_active (
			GTK_TOGGLE_BUTTON (dialog->radio_head));

	if (head_selected) {
		gtk_widget_set_sensitive (GTK_WIDGET (dialog->entry_revision),
		                          FALSE);
		gtk_widget_set_sensitive (GTK_WIDGET (dialog->button_select_revision),
		                          FALSE);
	} else {
		gtk_widget_set_sensitive (GTK_WIDGET (dialog->entry_revision),
		                          TRUE);
		gtk_widget_set_sensitive (GTK_WIDGET (dialog->button_select_revision),
		                          TRUE);
	}
}

static void checkout_destroy_cb             (GtkWidget *widget,
                                             NautilusSvnCheckout *dialog)
{
	g_free (dialog);
}

void
nautilus_svn_checkout_show (GtkWidget *parent,
                            const gchar *dir)
{
	NautilusSvnCheckout *dialog;
	GladeXML *xml;
	GtkWidget *button_browse_repo;
	GtkWidget *button_browse_dir;
	GtkWidget *button_ok;
	GtkWidget *button_close;

	dialog = g_new0 (NautilusSvnCheckout, 1);

	xml = glade_xml_new (GLADEDIR "/" "nautilus-svn-checkout.glade",
			             NULL, NULL);
	dialog->window = glade_xml_get_widget (xml, "dialog_checkout");

	g_signal_connect (dialog->window,
	                  "destroy",
	                  G_CALLBACK (checkout_destroy_cb),
	                  dialog);

	dialog->combo_repo_url = GTK_COMBO_BOX_ENTRY (
			glade_xml_get_widget (xml, "combo_repo_url"));

	dialog->combo_local_dir = GTK_COMBO_BOX_ENTRY (
			glade_xml_get_widget (xml, "combo_local_dir"));
	gtk_entry_set_text (
			GTK_ENTRY (GTK_BIN (dialog->combo_local_dir)->child), dir);

	button_browse_dir = glade_xml_get_widget(xml, "button_browse_dir");
	g_signal_connect (button_browse_dir,
	                  "clicked",
	                  G_CALLBACK (button_browse_dir_clicked_cb),
	                  dialog);

	button_browse_repo = glade_xml_get_widget(xml, "button_browse_repo");
	g_signal_connect (button_browse_repo,
	                  "clicked",
	                  G_CALLBACK (button_browse_repo_clicked_cb),
	                  dialog);

	dialog->radio_head = GTK_RADIO_BUTTON (
			glade_xml_get_widget (xml, "radio_head"));
	g_signal_connect (dialog->radio_head,
	                  "toggled",
	                  G_CALLBACK (radio_group_toggled_cb),
	                  dialog);

	dialog->radio_revision = GTK_RADIO_BUTTON (
			glade_xml_get_widget (xml, "radio_revision"));
	g_signal_connect (dialog->radio_revision,
	                  "toggled",
	                  G_CALLBACK (radio_group_toggled_cb),
	                  dialog);

	dialog->entry_revision = GTK_ENTRY (
			glade_xml_get_widget (xml, "entry_revision"));

	dialog->button_select_revision = GTK_BUTTON (
			glade_xml_get_widget (xml, "button_select_revision"));

	button_ok = glade_xml_get_widget (xml, "button_ok");
	g_signal_connect (button_ok,
	                  "clicked",
	                  G_CALLBACK (button_ok_clicked_cb),
	                  dialog);

	button_close = glade_xml_get_widget (xml, "button_close");
	g_signal_connect (button_close,
	                  "clicked",
	                  G_CALLBACK (button_close_clicked_cb),
	                  dialog);

	g_object_unref(xml);

	gtk_widget_show (dialog->window);
}
